function prepare(){
	if(!Array.prototype.indexOf) {
    		Array.prototype.indexOf = function(needle) {
        		for(var i = 0; i < this.length; i++) {
            			if(this[i] === needle) {
               	 		return i;
            			}
        		}
        	return -1;
    		};
	}

	String.format = function(src){
    		if (arguments.length == 0) return null;
    		var args = Array.prototype.slice.call(arguments, 1);
    		return src.replace(/\{(\d+)\}/g, function(m, i){
        	return args[i];
    		});
	};

}

function itemEntity(title,id,parentId,level){
		this.title = title;
		this.id = id;
		this.parentId = parentId ;
		this.subItems = new Array();
		this.level = level;
	}

function Data(){
	this.item1 = new itemEntity("1",1,0,1);
	this.item2 = new itemEntity("2",2,3,3);
	this.item3 = new itemEntity("3",3,1,2);
	this.item4 = new itemEntity("4",4,0,1);
	this.item5 = new itemEntity("5",5,3,3);
	this.item6 = new itemEntity("6",6,5,4);
	this.itemList = new Array();
	this.itemList.push(this.item1);
	this.itemList.push(this.item2);
	this.itemList.push(this.item3);
	this.itemList.push(this.item4);
	this.itemList.push(this.item5);
	this.itemList.push(this.item6);
}

function getSubItems(parentItem,itemList,itemAdded){
	if(itemAdded.indexOf(parentItem.id) > 0)
			return;
	for(var i = 0;i < itemList.length;++i){
		if(itemList[i].parentId == parentItem.id){
			parentItem.subItems.push(itemList[i]);
			getSubItems(itemList[i],itemList,itemAdded);
		}	
	}
	itemAdded.push(parentItem.id);
}

function retriveDataFromHtml(){
	var itemsTable = $("#itemstb");
	var trs = $("#itemstb tr");
	
}

function formatItemList(itemList){
	//debugger;
	var formattedList = new Array();
	var itemAdded = new Array();
	for(var i = 0;i < itemList.length;++i){
		getSubItems(itemList[i],itemList,itemAdded);
		if(itemList[i].parentId == 0){
			formattedList.push(itemList[i]);
		}
	}

	return formattedList;
}


function createTree(data,containerId){
	prepare();
	
	//var data = new Data();
	var list = formatItemList(data.itemList);
	var menuDiv = $("#" + containerId);
	for(var i = 0; i < list.length;++i){
		var container = createMainContainer(list[i],menuDiv);
		createItems(list[i],container);	
	}
}

function createMainContainer(item,container){
	var div = $("<div/>").attr("id",item.id);
	var inputStr = String.format('<input type="hidden" id="' + item.id + "_status" + '" name="{0}" value="1" />',"node_"+item.id+"_ status");
	var input = $(inputStr);
	div.append(input);
	//$("#myMenu").append(div);
	container.append(div);
	return div;
}

function createItems(item,mainContainer){
	getTitleSpan(item,mainContainer);
        getTitleArrow(item,mainContainer);
        addItems(item,mainContainer);
}

function createCheckBox(item){
	var html = '<input type="checkbox" id="cb' + item.id +'" value="'+ item.title +'" /> <label for="cb'+item.id+'">'+ item.title +'</label>';
	return html;
}

// title means first level title
function getTitleArrow(item,mainContainer){
	var s = '<a href="#" class="link_text"><span class="node_title" id="{0}">&#9660;</span></a>';
	s = String.format(s, item.id+ "_arrow");
	var a = $(s);
	a.click(function (){ hide_menu(item.id);});
	mainContainer.prepend(a);
}

function getTitleSpan(item,mainContainer){
	var titleStr;
	var title;
	if(item.level != 3){
		titleStr = String.format('<span class="node_title" id="{1}">{0}</span>',item.title,item.id + "_title");
		title = $(titleStr);
	}
	else{
		titleStr = String.format('<span class="node_title" id="{1}">{0}</span>', createCheckBox(item),item.id + "_title");
		title = $(titleStr);
		
	}
	mainContainer.prepend(title);
}

function hide_menu(node_id){
	var node = document.getElementById(node_id + "_container");
	
	var node_status = document.getElementById(node_id + "_title");

	var node_text=document.getElementById(node_id+"_arrow");
	if(node_status.value=="1" && node != null){
		node.style.display = "none"; 
	}
	else if(node != null){
		
		node.style.display = ""; 
	}

	if(node_status.value=="1" && node != null){
		node_status.value="0";
		node_text.innerHTML="&#9658";
	}
	else{
		node_status.value="1";
		node_text.innerHTML="&#9660";
	}

	return false;

	}


function addItems(item,mainContainer){
     if(item.subItems.length  == 0) return;
     var containerNode;
     if ($("#" + item.id + "container").length == 0){
	containerNode = $("<div/>").attr("id",item.id + "_container");
	mainContainer.append(containerNode);	
     }
     
     for(var i = 0; i < item.subItems.length; ++ i){
	var subIdStr = item.subItems[i].id;
	var div = $("<div/>").attr("id", subIdStr).attr("class","node_content");
	createItems(item.subItems[i],div);
	containerNode.append(div);
     }     
}

//jQuery(document).ready(function() {
  //createTree(); 
//});
